package idv.takeshi.software.productline.bookstore.domain.model.useraccount;

import idv.takeshi.software.productline.bookstore.domain.model.order.Order;

import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;

import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.OneToMany;
import javax.persistence.OrderBy;

import org.apache.commons.lang.Validate;
import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;

/**
 * A model class which represents as a Customer, kind of user account role.
 * @author takeshi
 *
 */
@Entity
public class Customer extends Role {

	private static final String ROLE_NAME = "ROLE_CUSTOMER";
	
	@OneToMany
	@JoinColumn(name = "CUSTOMER_ID")
	@OrderBy
	private Set<Order> orders = new LinkedHashSet<Order>();
	
	/**
	 * Add an order for this customer.
	 * @param order
	 */
	public void addOrder(Order order){
		Validate.notNull(order, "order shall always not be null");
		this.orders.add(order);
	}
	 
	
	/**
	 * Get unmodifiable orders.
	 * @return the orders
	 */
	public Set<Order> getOrders() {
		return Collections.unmodifiableSet(orders);
	}

	/* (non-Javadoc)
	 * @see idv.takeshi.software.productline.bookstore.model.useraccount.Role#getRoleName()
	 */
	@Override
	public String getRoleName() {
		return ROLE_NAME;
	}
	
	/* (non-Javadoc)
	 * @see java.lang.Object#equals(java.lang.Object)
	 */
	@Override
	public boolean equals(Object obj) {
		if (obj == null) { return false; }
		   if (obj == this) { return true; }
		   if (obj.getClass() != getClass()) {
		     return false;
		   }
		   Customer rhs = (Customer) obj;
		   return new EqualsBuilder()
			   .appendSuper(super.equals(rhs))
			   .append(this.orders, rhs.orders)
		       .isEquals();
	}


	/* (non-Javadoc)
	 * @see idv.takeshi.software.productline.bookstore.domain.model.useraccount.Role#hashCode()
	 */
	@Override
	public int hashCode() {
		return new HashCodeBuilder(11, 97).
		   appendSuper(super.hashCode()).
	       append(this.orders).
	       toHashCode();
	}

}
 
